package com.nbsaas.lbs.qq.v1.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nbsaas.http.Connection;
import com.nbsaas.http.HttpConnection;
import com.nbsaas.lbs.qq.v1.builder.ServicesBuilder;
import com.nbsaas.lbs.qq.v1.domain.response.AreaItem;
import com.nbsaas.lbs.qq.v1.domain.response.Areas;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class DistrictService extends BaseService {
  
  String prons = "[{\"id\":\"110000\",\"name\":\"北京\",\"fullname\":\"北京市\",\"location\":{\"lat\":39.90469,\"lng\":116.40717}},{\"id\":\"120000\",\"name\":\"天津\",\"fullname\":\"天津市\",\"location\":{\"lat\":39.0851,\"lng\":117.19937}},{\"id\":\"130000\",\"name\":\"河北\",\"fullname\":\"河北省\",\"location\":{\"lat\":38.03599,\"lng\":114.46979}},{\"id\":\"140000\",\"name\":\"山西\",\"fullname\":\"山西省\",\"location\":{\"lat\":37.87343,\"lng\":112.56272}},{\"id\":\"150000\",\"name\":\"内蒙古\",\"fullname\":\"内蒙古自治区\",\"location\":{\"lat\":40.81733,\"lng\":111.76522}},{\"id\":\"210000\",\"name\":\"辽宁\",\"fullname\":\"辽宁省\",\"location\":{\"lat\":41.83571,\"lng\":123.42925}},{\"id\":\"220000\",\"name\":\"吉林\",\"fullname\":\"吉林省\",\"location\":{\"lat\":43.89616,\"lng\":125.3268}},{\"id\":\"230000\",\"name\":\"黑龙江\",\"fullname\":\"黑龙江省\",\"location\":{\"lat\":45.74208,\"lng\":126.66285}},{\"id\":\"310000\",\"name\":\"上海\",\"fullname\":\"上海市\",\"location\":{\"lat\":31.23037,\"lng\":121.4737}},{\"id\":\"320000\",\"name\":\"江苏\",\"fullname\":\"江苏省\",\"location\":{\"lat\":32.06071,\"lng\":118.76295}},{\"id\":\"330000\",\"name\":\"浙江\",\"fullname\":\"浙江省\",\"location\":{\"lat\":30.26555,\"lng\":120.1536}},{\"id\":\"340000\",\"name\":\"安徽\",\"fullname\":\"安徽省\",\"location\":{\"lat\":31.86157,\"lng\":117.28565}},{\"id\":\"350000\",\"name\":\"福建\",\"fullname\":\"福建省\",\"location\":{\"lat\":26.09982,\"lng\":119.29659}},{\"id\":\"360000\",\"name\":\"江西\",\"fullname\":\"江西省\",\"location\":{\"lat\":28.67417,\"lng\":115.91004}},{\"id\":\"370000\",\"name\":\"山东\",\"fullname\":\"山东省\",\"location\":{\"lat\":36.66826,\"lng\":117.02076}},{\"id\":\"410000\",\"name\":\"河南\",\"fullname\":\"河南省\",\"location\":{\"lat\":34.76571,\"lng\":113.75322}},{\"id\":\"420000\",\"name\":\"湖北\",\"fullname\":\"湖北省\",\"location\":{\"lat\":30.54539,\"lng\":114.34234}},{\"id\":\"430000\",\"name\":\"湖南\",\"fullname\":\"湖南省\",\"location\":{\"lat\":28.11266,\"lng\":112.9834}},{\"id\":\"440000\",\"name\":\"广东\",\"fullname\":\"广东省\",\"location\":{\"lat\":23.13171,\"lng\":113.26627}},{\"id\":\"450000\",\"name\":\"广西\",\"fullname\":\"广西壮族自治区\",\"location\":{\"lat\":22.81521,\"lng\":108.32754}},{\"id\":\"460000\",\"name\":\"海南\",\"fullname\":\"海南省\",\"location\":{\"lat\":20.01997,\"lng\":110.34863}},{\"id\":\"500000\",\"name\":\"重庆\",\"fullname\":\"重庆市\",\"location\":{\"lat\":29.56471,\"lng\":106.55073}},{\"id\":\"510000\",\"name\":\"四川\",\"fullname\":\"四川省\",\"location\":{\"lat\":30.65089,\"lng\":104.07572}},{\"id\":\"520000\",\"name\":\"贵州\",\"fullname\":\"贵州省\",\"location\":{\"lat\":26.5982,\"lng\":106.70722}},{\"id\":\"530000\",\"name\":\"云南\",\"fullname\":\"云南省\",\"location\":{\"lat\":25.0453,\"lng\":102.70973}},{\"id\":\"540000\",\"name\":\"西藏\",\"fullname\":\"西藏自治区\",\"location\":{\"lat\":29.64725,\"lng\":91.11748}},{\"id\":\"610000\",\"name\":\"陕西\",\"fullname\":\"陕西省\",\"location\":{\"lat\":34.26486,\"lng\":108.95424}},{\"id\":\"620000\",\"name\":\"甘肃\",\"fullname\":\"甘肃省\",\"location\":{\"lat\":36.05942,\"lng\":103.82634}},{\"id\":\"630000\",\"name\":\"青海\",\"fullname\":\"青海省\",\"location\":{\"lat\":36.62087,\"lng\":101.78011}},{\"id\":\"640000\",\"name\":\"宁夏\",\"fullname\":\"宁夏回族自治区\",\"location\":{\"lat\":38.47117,\"lng\":106.25867}},{\"id\":\"650000\",\"name\":\"新疆\",\"fullname\":\"新疆维吾尔自治区\",\"location\":{\"lat\":43.79343,\"lng\":87.6271}},{\"id\":\"710000\",\"name\":\"台湾\",\"fullname\":\"台湾省\",\"location\":{\"lat\":25.030724,\"lng\":121.52007}},{\"id\":\"810000\",\"name\":\"香港\",\"fullname\":\"香港特别行政区\",\"location\":{\"lat\":22.27534,\"lng\":114.16546}},{\"id\":\"820000\",\"name\":\"澳门\",\"fullname\":\"澳门特别行政区\",\"location\":{\"lat\":22.19875,\"lng\":113.54913}}]";
  
  public DistrictService(String key) {
    super(key);
  }
  
  public Areas child(String id) {
    Connection connection = HttpConnection.connect("https://apis.map.qq.com/ws/district/v1/getchildren");
    connection.data("key", key);
    connection.data("id", id);
    return getAreas(connection);
  }
  
  public Areas list() {
    Connection connection = HttpConnection.connect("https://apis.map.qq.com/ws/district/v1/list");
    connection.data("key", key);
    return getAreas(connection);
  }
  
  
  private Areas getAreas(Connection connection) {
    Areas areas = null;
    try {
      String body = connection.execute().body();
      System.out.println(body);
      areas = new Gson().fromJson(body, Areas.class);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return areas;
  }
  
  public List<AreaItem> china() {
    List<AreaItem> result = new ArrayList<>();
    Type type = new TypeToken<List<AreaItem>>() {
    }.getType();
    result = new Gson().fromJson(prons, type);
    
    return result;
  }
  
  public static void main(String[] args) throws InterruptedException {
    DistrictService service = getDistrictService();
    List<AreaItem> items = service.china();
    for (AreaItem item : items) {
      System.out.println(service.child(item.getId()));
      Thread.sleep(210);
    }
    // System.out.println(areas);
  }
  
  private static DistrictService getDistrictService() {
    return ServicesBuilder.newBuilder().key("H4DBZ-WLVCU-YLEVF-4MIDF-MGB5H-TOFDR").build().getDistrictService();
  }
  
  private static void f() {
    DistrictService service = getDistrictService();
    Areas areas = service.list();
    StringBuffer buffer = new StringBuffer();
    List<AreaItem> items = areas.getResult().get(0);
    for (AreaItem item : items) {
      buffer.append(" AreaItem item = new AreaItem();\n");
      buffer.append(" item.setId(\"" + item.getId() + "\");\n");
      buffer.append(" item.setFullname(\"" + item.getFullname() + "\");\n");
      buffer.append(" item.setName(\"" + item.getName() + "\");\n");
      buffer.append(" Location location = new Location();\n");
      buffer.append(" location.setLat(" + item.getLocation().getLat() + ");\n");
      buffer.append(" location.setLng(" + item.getLocation().getLng() + ");\n");
      buffer.append(" item.setLocation(location);\n");
      buffer.append(" result.add(item);\n");
    }
    System.out.println(new Gson().toJson(items));
  }
  
  private static void findbyId() {
    DistrictService service = getDistrictService();
    Areas areas = service.child("110000");
    System.out.println(areas);
  }
}
